﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSD.OneStopService.BLL;

public partial class Modules_ChuyenXuLy : System.Web.UI.Page
{
    private StaffRepository resStaff = new StaffRepository();// Repository danh mục cán bộ
    private ProfileStoreRepository resProfileStore;// Repository Hồ sơ dịch vụ
    private MixProfileRepository resMixProfile = new MixProfileRepository();// Repository Hồ sơ cần phối hợp xử lý
    private MixDetailsRepository resMixDetail = new MixDetailsRepository();// Repository Chi tiết phối hợp xử lý
    private ProfileHistoryRepository resHistory = new ProfileHistoryRepository();// Repository Lịch sử tiếp nhận, xử lý và trả hồ sơ

    private List<Staff> stafflist;
    private List<Staff> stafflistInDept;
    public int profileID = 0;

    string mixListSelected = string.Empty;// Danh sách cán bộ xử lý phối hợp (tạm)


    protected void Page_Load(object sender, EventArgs e)
    {
        ContextUser.UserName = Convert.ToString(HttpContext.Current.Session["AdminLogined"]);
        string ItemID = Request.QueryString["id"].ToString();
        resProfileStore = new ProfileStoreRepository();

        if (!IsPostBack)
        {
            
            if (ItemID != null)
            {
                ProfileStoreItem.profileItem = resProfileStore.Get(int.Parse(ItemID));
                stafflist = resStaff.GetAll();
                this.lbTitle.Text = ProfileStoreItem.profileItem.ServiceName;
                this.lbHandOverDate.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Today);

                BindGridMixUser(ContextUser.CurrentUser.Name);

                BindCboXuLyChinh();
                profileID = ProfileStoreItem.profileItem.ID;
            }
        }
    }

    public void Page_PreRender(object sender, EventArgs e)
    {
        //BindGridMixUser(ContextUser.CurrentUser.Name);
    }

    private void BindCboXuLyChinh()
    {
        if (ProfileStoreItem.profileItem != null)
        {
            stafflistInDept = resStaff.GetStaffByDepartment(ProfileStoreItem.profileItem.Service.DepartmentID).FindAll(r => !string.IsNullOrEmpty(r.UserName));
            
            cboXuLyChinh.DataSource = stafflistInDept;
            cboXuLyChinh.DataTextField = "Name";
            cboXuLyChinh.DataValueField = "UserName";
            cboXuLyChinh.DataBind();
        }
    }

    private void BindGridMixUser(string userName)
    {
        mixListSelected = string.Empty;
        try
        {
            for (int i = 0; i < rptMixUsers.Items.Count; i++)
            {
                if (rptMixUsers.Items[i].FindControl("chkChon") != null && rptMixUsers.Items[i].FindControl("chkChon").ToString() == "True")
                {
                    Staff selectedMix = (Staff)rptMixUsers.Items[i].DataItem;
                    mixListSelected += selectedMix.UserName + ",";
                }
            }

            rptMixUsers.DataSource = stafflist.FindAll(s => (s.UserName != userName) && (s.UserName != mixListSelected));
            rptMixUsers.DataBind();
        }
        catch { }
    }

    private void BindTxtContent()
    {
        string contentText = string.Empty;
        string mixUsers = string.Empty;
        string name = cboXuLyChinh.SelectedItem.Text;
        contentText += "Chuyển: " + name + " xử lý hồ sơ." + Environment.NewLine;
        try
        {
            int count = 0;
            for (int i = 0; i < rptMixUsers.Items.Count; i++)
            {
                Staff selectedMix = (Staff)rptMixUsers.Items[i].DataItem;
                CheckBox chkBox = ((CheckBox)rptMixUsers.Items[i].FindControl("chkChon"));
                Label lblName = (Label)rptMixUsers.Items[i].FindControl("lblName");
                if (chkBox != null && chkBox.Checked == true)
                {
                    count++;
                    mixUsers += count.ToString() + "; " + lblName.Text + Environment.NewLine;
                }
            }
        }
        catch { }
        if (!string.IsNullOrEmpty(mixUsers))
        {
            contentText += "Danh sách cán bộ phối hợp xử lý" + Environment.NewLine;
            contentText += mixUsers;
        }

        contentText += "Yêu cầu hoàn thành đúng thời hạn quy định.";
        txtContent.Text = contentText;

    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
    }

    protected void rptMixUsers_Load(object sender, EventArgs e)
    {
        BindTxtContent();
    }
}